﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using ChangeHope.Common;

namespace YXShop.Web.admin.templates
{
    public partial class style_edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.IsLiteralContent();
            }
        }
        /// <summary>
        /// 验证
        /// </summary>
        private void InitWebControl()
        {
            ChangeHope.WebPage.WebControl.Validate(this.txtName, "输入模板名称", "isnull", "必填", "该项为必填项");
            this.Form.Attributes.Add("onsubmit", "return CheckForm()");
        }

        protected void button2_Click(object sender, EventArgs e)
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            YXShop.BLL.SystemInfo.ShopStyle bll = new YXShop.BLL.SystemInfo.ShopStyle();
            YXShop.Model.SystemInfo.ShopStyle model = new YXShop.Model.SystemInfo.ShopStyle();
            string templateName = this.txtName.Text;
            model.Name = templateName;
            ChangeHope.Common.UploadFile uf = new ChangeHope.Common.UploadFile();
            uf.ExtensionLim = ".gif,.jpg,.jpeg,.dmp";
            uf.FileLengthLim = sp.ImageSize;
            uf.PostedFile = this.fuImages;
            uf.SavePath = "/yxuploadfile/other";
            if (uf.Upload())
            {
                if (uf.HaveLoad)
                {
                    model.SampleMapPath = uf.FilePath;
                }
                else
                {
                    this.ltlMsg.Text = "操作失败，模板图样"+uf.Message;
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
            }

            ChangeHope.Common.UploadFile ufTemplate = new ChangeHope.Common.UploadFile();
            ufTemplate.ExtensionLim = ".zip";
            ufTemplate.FileLengthLim = 100000;
            ufTemplate.PostedFile = this.fuTemplate;
            ufTemplate.SavePath = "/templates/yxshop_template";
            if (ufTemplate.Upload())
            {
                if (ufTemplate.HaveLoad)
                {
                    string path = ufTemplate.FilePath;
                    string zipFile = Request.ServerVariables["APPL_PHYSICAL_PATH"] + path.Replace("/", "\\");
                    ZipHelper zh = new ZipHelper();
                    if (zh.UnZip(zipFile, Request.ServerVariables["APPL_PHYSICAL_PATH"] + "templates/yxshop_template/"))
                    {
                        ChangeHope.Common.FileHelper fh = new FileHelper();
                        if (fh.DeleteFile(Server.MapPath("~\\" + path)))
                        {
                            string fileName = ufTemplate.PostedFile.FileName;
                            model.TemplatePath = "templates/yxshop_template/" + fileName.Substring(0, fileName.LastIndexOf('.'));
                        }
                        else
                        {
                            this.ltlMsg.Text = "操作失败，模板上传失败.";
                            this.pnlMsg.Visible = true;
                            this.pnlMsg.CssClass = "actionErr";
                            return;
                        }
                    }
                    else
                    {
                        this.ltlMsg.Text = "操作失败，模板上传失败.";
                        this.pnlMsg.Visible = true;
                        this.pnlMsg.CssClass = "actionErr";
                        return;
                    }
                }
                else
                {
                    this.ltlMsg.Text = "操作失败，模板" + ufTemplate.Message;
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
            }
            else
            {
                this.ltlMsg.Text = "操作失败，" + ufTemplate.Message;
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
                return;
            }
            model.IsDefault = 0;
            model.Version = ChangeHope.Common.ServerInfo.VersionInformation();
            model.CreateTime = System.DateTime.Now;
            if (bll.Add(model) > 0)
            {
                ChangeHope.WebPage.BasePage.PageRight("操作成功，模板上传成功","template.aspx");
            }

        }
    }
}
